package demo;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;

public class demo implements ActionListener {
    JTextArea inputText, showText;
    File fWirte = new File("thing.txt");

    public static void main(String args[]) {
        WindowDocument win = new WindowDocument();
        win.setBounds(100, 100, 590, 500);
        win.setTitle("排序单词");
    }

    public void setInputText(JTextArea text) {
        inputText = text;
    }

    public void setShowText(JTextArea text) {
        showText = text;
    }

    public void actionPerformed(ActionEvent e) {
        String str = e.getActionCommand();
        if (str.equals("copy"))
            showText.copy();
        else if (str.equals("cut"))
            showText.cut();
        else if (str.equals("paste"))
            inputText.paste();
        else if (str.equals("save")) {
            try {
                Writer out = new FileWriter(fWirte);
                BufferedWriter bufferWrite = new BufferedWriter(out);
                bufferWrite.write(showText.getText());
                bufferWrite.newLine();
                bufferWrite.close();
                out.close();
                JOptionPane.showMessageDialog(null, "！！！保存成功！！！");
            } catch (IOException e1) {
                System.out.println(e1.toString());
            }
        } else if (str.equals("open")) {
            JOptionPane.showMessageDialog(null, "！！！文件打开成功！！！");
            try {
                Reader in = new FileReader(fWirte);
                BufferedReader bufferRead = new BufferedReader(in);
                String tem = null;
                while ((tem = bufferRead.readLine()) != null) {
                    StringTokenizer fenxi = new StringTokenizer(str);
                    int count = fenxi.countTokens();
                    inputText.setText(tem);
                    System.out.println(" 句子中单词个数:" + count);
                }
                bufferRead.close();
                in.close();
            } catch (IOException ea) {
                System.out.println(ea.toString());
            }
        } else if (str.equals("replace")) {
            String inputValue = JOptionPane.showInputDialog("Please input a value to replace!");
            String inputValue2 = JOptionPane.showInputDialog("Change to what!");
            inputText.setText(inputText.getText().replace(inputValue, inputValue2));
        } else if (str.equals("sum")) {
            String regex = "[\\u4e00-\\u9fa5]+";
            int n1 = 0;// 中文
            int n2 = 0;// 英文
            int n3 = 0;// 数字
            int n4 = 0;// 特殊字符
            for (int i = 0; i < inputText.getText().length(); i++) {
                char c = inputText.getText().charAt(i);
                if (Character.toString(c).matches(regex))
                    n1++;
                else if (c >= '0' && c <= '9')
                    n3++;
                else if ((c >= 'a' && c < 'z') || (c >= 'A' && c < 'Z'))
                    n2++;
                else
                    n4++;
            }
            System.out.println(" 句中中文字符个数:" + n1 + ",数字字符个数:" + n3 + ",英文字符个数:" + n2 + ",特殊字符个数:" + n4);
        }
    }

}

class TextListener implements DocumentListener {
    JTextArea inputText, showText;

    public void setInputText(JTextArea text) {
        inputText = text;
    }

    public void setShowText(JTextArea text) {
        showText = text;
    }

    public void changedUpdate(DocumentEvent e) {
        String str = inputText.getText();
        // 空格、数字和符号(!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~)组成的正则表达式:
        String regex = "[\\s\\d\\p{Punct}]+";
        String words[] = str.split(regex);
        Arrays.sort(words); // 按字典序从小到大排序
        showText.setText(null);
        for (int i = 0; i < words.length; i++)
            showText.append(words[i] + ",");
    }

    @Override
    public void insertUpdate(DocumentEvent e) {
        // TODO Auto-generated method stub
        changedUpdate(e);
    }

    @Override
    public void removeUpdate(DocumentEvent e) {
        // TODO Auto-generated method stub
        changedUpdate(e);
    }
}

